/**
 * Created by chenzheyu on 2017/4/11.
 */


$(function () {


    var SEOval = '';
    var limit = 20;


    function xin() {
        $.ajax({
            url: ctx + '/rentorder/selectRentOrdersCount',
            type: 'GET',
            success: function (data) {

                var text = data.split(',');

                var li=$('.CustomTabs ul li');

                if(text[0]>0){
                    if(li.eq(0).find('.CustomBadge').length>0){
                        li.eq(0).find('.CustomBadge').remove();
                        li.eq(0).Badge({data:{text:text[0]>99?'99+':text[0],state:'No'}})
                    }else {
                        li.eq(0).Badge({data:{text:text[0]>99?'99+':text[0],state:'No'}})
                    }
                }

                if(text[1]>0){
                    if(li.eq(7).find('.CustomBadge').length>0){
                        li.eq(7).find('.CustomBadge').remove();
                        li.eq(7).Badge({data:{text:text[1]>99?'99+':text[1],state:'No'}})
                    }else {
                        li.eq(7).Badge({data:{text:text[1]>99?'99+':text[1],state:'No'}})
                    }
                }

                if(text[2]>0){
                    if(li.eq(8).find('.CustomBadge').length>0){
                        li.eq(8).find('.CustomBadge').remove();
                        li.eq(8).Badge({data:{text:text[2]>99?'99+':text[2],state:'No'}})
                    }else {
                        li.eq(8).Badge({data:{text:text[2]>99?'99+':text[2],state:'No'}})
                    }
                }
            }
        })
    }
    xin();
    setInterval(function () {
        xin()
    }, 60000);


    $(".Nav .SEO .SEOButton").on("click", function () {
        SEOval = $(".Nav .SEO  input").val();
        GaleDayRemind(1, SEOval);
    });
    $(document).keyup(function (event) {
        switch (event.keyCode) {
            case 13:
                $(".Nav .SEO .SEOButton").click();
                break;
        }
    });
    GaleDayRemind(1, SEOval);
    function GaleDayRemind(page, inquireVal) {
        var FreshOrder = {
            plateNoOrPassengerPhone: inquireVal || "",
            status: 50,
            limit: limit,
            "pager.offset": (page - 1) * limit
        };
        $.ajax({
            url: ctx + '/rentorder/selectRentOrders',
            type: "GET",
            data: FreshOrder,
            success: function (data) {
                var option = {
                    data: {
                        Thead: {
                            PullDownAction: '操作',
                            status: '订单状态',
                            OrderNumber: "单号",
                            totalFree:'总金额',
                            payed: '租金',
                            Store: "门店",
                            RetrievingMeans: "取车地址",
                            ReturnType: "还车地址",
                            HireTime: "预约时间",
                            ReturnTime: "还车时间",
                            ResidueTime: "剩余时间",
                            ClientName: "客户名称",
                            ClientPhone: "客户电话",
                            Car: "车辆号牌",
                            CarType: "车辆类型",
                            // RentalStandard: "租金标准",
                            Prepayment: "租车押金",
                            prepayRentTime: "预授权到期时间",
                            RemainingTime: "预授权剩余时间",
                            // ViolationMoneys: "违章押金",
                            // PrepayMoney: "预授金额",
                            SubordinateCompanies: "所属公司",
                            Remark: "备注"
                        },
                        Tbody: []
                    }
                };

                // console.log(data.content);

                var PresentTimestamp = Date.parse(new Date()) / 1000;
                for (var i = 0; i < data.content.length; i++) {
                    (function () {
                        var index = i;
                        var val = {
                            id: data.content[i].id,
                            PullDownAction: {
                                Default:{
                            name: '退还租车押金',
                                action: function () {
                                var ActionOption = {
                                    data: {
                                        Title: '退还租车押金提示',
                                        Content: '是否要退还该车的租车押金？',
                                        OkChange: function () {
                                            $.ajax({
                                                url: ctx + '/rentorder/backForegift',
                                                data: {
                                                    id: data.content[index].id,
                                                    type: 0
                                                },
                                                type: 'POST',
                                                success: function (data) {

                                                    if (data.message == '退还成功') {
                                                        $('body').Overlays({
                                                            data: {
                                                                text: "退还租车押金成功！",
                                                                state: "Ok"
                                                            }
                                                        });
                                                        GaleDayRemind(1, SEOval);
                                                        xin();
                                                    } else {
                                                        $('body').Overlays({
                                                            data: {
                                                                text: "退还租车押金失败，请稍后重试！",
                                                                state: "Ok"
                                                            }
                                                        });
                                                    }
                                                }
                                            })
                                        }
                                    }
                                };

                                $('body').ModalBox(ActionOption);
                            }
                        },
                                data: [
                                    {
                                        name: '查看',
                                        action: function (e) {
                                            window.location.href = ctx + '/rentorder/orderinfo?id=' + data.content[index].id;
                                        }
                                    }
                                ]
                            },
                            status:'',
                            OddNumbers: data.content[i].orderNo,
                            totalFree:data.content[i].totalFee,
                            payed: '',
                            ShopName: data.content[i].shopName,
                            RetrievingMeans: data.content[index].takeCarWay == 1 ? data.content[index].startAddress : "门店自取",
                            ReturnType: data.content[index].backCarWay == 1 ? data.content[index].endAddress : '门店自还',
                            CarTime: data.content[i].bookTime ? $().Timestamp.TimestampDate(data.content[i].bookTime) : '无',
                            LeaseTime: data.content[i].rentTime ? $().Timestamp.TimestampDate(data.content[i].rentTime) : '无',

                            ResidueTime: $().Timestamp.DayCalculate(data.content[index].reRentTime ? data.content[index].reRentTime - PresentTimestamp : data.content[index].rentTime - PresentTimestamp),
                            CustomerName: data.content[i].passengerName,
                            CustomerPhone: data.content[i].passengerPhone,
                            PlateNumbers: data.content[i].plateNo?data.content[i].plateNo:'未派车',
                            VehicleModel: data.content[i].category,
                            // CharterBase: data.content[i].CharterBase,
                            PaysTheRent: data.content[i].prepayRent,
                            // ViolationOfTheDeposit: data.content[i].peccancyForegift,
                            prepayRentTime:data.content[i].prepayRentTime? $().Timestamp.TimestampDate(data.content[i].prepayRentTime):'无',
                            RemainingTime: data.content[index].prepayRentTime?$().Timestamp.DayCalculate(data.content[index].prepayRentTime - PresentTimestamp):'无',
                            GrantAmountInAdvance: data.content[i].companyName,

                            // SubordinateCompanies: data.data[i].SubordinateCompanies,
                            Remark: data.content[i].memo
                        };
                        switch (data.content[index].status) {
                            case 0:
                                val.status = '新单';
                                break;
                            case 5:
                                val.status = '已支付';
                                break;
                            case 10:
                                val.status = '已确认';
                                break;
                            case 15:
                                val.status = '已派单';
                                break;
                            case 20:
                                val.status = '服务中';
                                break;
                            case 25:
                                val.status = '申请续租';
                                break;
                            case 30:
                                val.status = '申请还车';
                                break;
                            case 35:
                                val.status = '已还车';
                                break;
                            case 40:
                                val.status = '已评价';
                                break;
                            case 45:
                                val.status = '已完成';
                                break;
                            case -5:
                                val.status = '已销单';
                                break;
                            default:
                                val.status = '未知';
                        }
                        switch (data.content[i].isPay) {
                            case 0:
                                val.payed = $('<span class="badge">未交付</span>');
                                break;
                            case 1:
                                val.payed = data.content[i].payed;
                                break;
                            case 2:
                                val.payed =data.content[i].payed;
                                break;
                            case 3:
                                val.payed = data.content[i].payed;
                                break;
                            case 4:
                                val.payed = '已结算';
                                break;
                        }
                        option.data.Tbody.push(val)
                    })()

                }
                var PagingMdOption = {
                    data: {
                        rows: 20,
                        nowrows: page,
                        Pages: '',
                        total: data.totalElements,
                        change: function (index) {
                            GaleDayRemind(index, SEOval);
                        }
                    }
                };
                PagingMdOption.data.Pages = Math.ceil((PagingMdOption.data.total / PagingMdOption.data.rows));
                $(".CompletedOrderContent").find('.CustomOrder').remove();
                $(".CompletedOrderContent").find('.PagingMdPagingMd').remove();
                $('.CompletedOrderContent').Order(option);
                $(".CompletedOrderContent").PagingMd(PagingMdOption)
            }
        })
    }

    $(document).keyup(function (event) {
        switch (event.keyCode) {
            case 13:
                $(".Nav .SEO .SEOButton").click();
                break;
        }
    });
});
